#include "ModuleState.h"
#include "Portfolio.h"

ModuleState::~ModuleState(void)
{
}

void ModuleState::triforce(int x, int y, int color, int distance)
{
	int expand = distance;

	if(distance > 0)
	{
		expand +=2;
	}
	else
	{
		expand -=2;
	}

	triangle(
		port->getGUtil()->getLevel(0),
		x, y-expand,
		x + expand, y,
		x - expand, y,
		makecol(0,0,0));

	triangle(
		port->getGUtil()->getLevel(0),
		x, y-distance,
		x + distance, y,
		x - distance, y,
		color);
}

void ModuleState::showInstructions()
{
	int linetrip;
	int moused = 0;
	int dx = BUTA_X - mouse_x;
	int dy = BUTA_Y - mouse_y;
	rectfill(
		port->getGUtil()->getLevel(0),
		BUTA_X,
		BUTA_Y,
		BUTA_X + 701,
		BUTA_Y + (BUTB_Y - BUTA_Y)+1,
		C_TextBox);

	//Draw the horizontal scroll bar.
	vline(port->getGUtil()->getLevel(0), BUTA_X  , BUTA_Y, BUTB_Y, C_ScrollBar);
	vline(port->getGUtil()->getLevel(0), BUTA_X+1, BUTA_Y, BUTB_Y, C_ScrollBar);
	vline(port->getGUtil()->getLevel(0), BUTA_X-1, BUTA_Y, BUTB_Y, C_ScrollBar);

	if(line < MAXLINE)
	{
		linetrip = (BUTB_Y - BUTA_Y);
	}
	else
	{
		linetrip = (BUTB_Y - BUTA_Y)/(line-MAXLINE);
	}

	vline(port->getGUtil()->getLevel(0), 
		BUTA_X,
		BUTA_Y + linetrip * scroll_position,
		BUTA_Y + linetrip * scroll_position + linetrip,
		C_ScrollBarSmall);

	//CALC DISTANCE
	moused = (dx*dx) + (dy*dy);
	triforce(
		BUTA_X,
		BUTA_Y,
		C_ScrollBarButton);
	//TEXT SCROLLER
	if(moused < 49)
	{
		triforce(
			BUTA_X,BUTA_Y,
			C_ScrollBarButtonHit,
			-10);

		if(mouse_b == 1 && mouselock == 0 && line > MAXLINE)
		{
			scroll_position--;
			if(scroll_position < 0)
			{
				scroll_position = 0;
			}
			mouselock = 2;
		}
	}

	dx = BUTB_X - mouse_x;
	dy = BUTB_Y - mouse_y;

	//CALC DISTANCE
	moused = (dx*dx) + (dy*dy);
	triforce(
		BUTA_X, BUTB_Y,
		C_ScrollBarButton, -10);
	if(moused < 49)
	{
		triforce(
			BUTA_X,
			BUTB_Y,
			C_ScrollBarButtonHit,
			-10);
		if(mouse_b == 1 && mouselock == 0 && line > MAXLINE)
		{
			scroll_position++;
			if(scroll_position > (line-1) - MAXLINE)
			{
				scroll_position = (line-1) - MAXLINE;
			}
			mouselock = 2;
		}
	}

	//WHEEL MOUSE
	if(mouse_z < mouseoldz && line > MAXLINE)
	{
		scroll_position++;
		if(scroll_position > (line-1) - MAXLINE)
		{
			scroll_position = (line-1) - MAXLINE;
		}
		mouseoldz = mouse_z;
	}
	if(mouse_z > mouseoldz && line > MAXLINE)
	{
		scroll_position--;
		if(scroll_position < 0)
		{
			scroll_position = 0;
		}
		mouseoldz = mouse_z;
	}
	for(int i = scroll_position; i <= scroll_position + MAXLINE && i <= line ; i++)
	{
		port->getGUtil()->drawTextToBackBuffer(
			port->getRMgr()->font,
			helpText[i],
			70,
			(i - scroll_position) * 20 + BUTA_Y + 5,
			C_SetsumeiText);
	}
	port->getGUtil()->drawTextToBackBuffer(
		port->getRMgr()->font,
		defaultmsg[9],
		90, 490,
		C_SetsumeiText);
}

void ModuleState::checkMouse()
{
	buttonroll = 0;

	if(mouselock == 1)
	{
		if(mouse_b == 0)
		{
			mouselock = 0;
		}
	}
	if(mouselock == 2)
	{
		mousecounter++;
		if(mousecounter > 10)
		{
			mousecounter = 0;
			mouselock= 0;
		}
	}
	collided = check_pmask_collision(submenubutton_pmask,mousehit_pmask,38,510,mouse_x,mouse_y);
	if(collided == 1)
	{
		if(mouse_x < 90)
		{
			buttonroll = 1;
		}
		if(mouse_x > 100 && mouse_x < 220)
		{
			buttonroll = 2;
		}
		if(mouse_x > 248)
		{
			buttonroll = 3;
		}
	}
	else
	{
		buttonroll = 0;
	}

	if (mouse_b == 1 && mouselock == 0)
	{
		//Exit back to previous menu
		if(buttonroll == 1)
		{
			mouselock = 1;
			quit = 1;
		}
		if(buttonroll == 2)
		{
			mouselock = 1;
			state = 2;
		}
		if(buttonroll == 3)
		{
			mouselock = 1;
			state = 1;
		}
	}

	//quick exit
	if(mouse_b == 2 && mouselock == 0)
	{
		quit = 1;
	}
	mouseover = -1;
}

// a bit unusual
void ModuleState::drawButtons(unsigned int buttons)
{
	if(buttons & 1  && buttonroll == 1)
	{
		port->getGUtil()->blitToBackBuffer(
			(BITMAP *)port->getRMgr()->GraphicPackA[Submenubutton].dat,
			0, 0, 38, 510, 70, 76);
	}
	
	if(buttons & 2 && buttonroll == 2)
	{
		port->getGUtil()->blitToBackBuffer(
			(BITMAP *)port->getRMgr()->GraphicPackA[Submenubutton].dat,
			70, 0, 108, 510, 141, 76);
	}
	if(buttons & 4 && buttonroll == 3)
	{
		port->getGUtil()->blitToBackBuffer(
			(BITMAP *)port->getRMgr()->GraphicPackA[Submenubutton].dat,
			210, 0, 248, 510, 144, 76);
	}

}